#########################################################################
#
#    This file is part of LWX. 
#
#    LWX is the abbreviation of the Light WiMax simulation program which 
#    is a ns2 WiMAX network simulation module. LWX allows each user to 
#    apply his/her designed scheduling algorithm or other mechanism used 
#    in WiMAX network and generate the corresponding simulation results
#    for further analysis or other non-commercial purposes.
#
#    Copyright (C) 2008 Yen-Hung Cheng (pplong), NTUST in Taiwan, R.O.C.
#    E-Mail: lwx.ns2@gmail.com
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##########################################################################


# calculation each flow's average throughput and rate
# archetype

#############################
#  set and get parameters
#############################

# specify flow 

$_from = "0.0";
$_to   = "2.0";

# open log file
_log_file = File.open(ARGV[0].to_s, "r");

$_log_name   = ARGV[1].to_s;
if( $_log_name == "" ) then
(
  $_log_name = "log" ;
)
end

# store log to another log
$_log = File.open($_log_name, "w");

# init delay array
$_delay = [[0,0,0]];

#############################
#  calculation
#############################

# record each item
_log_file.each_line  { |line|
  (
    _str = line.split();
    
    # record sending time
    if( _str[0] == "s" &&  _str[30] == $_from && _str[32] == $_to) then
    ( 
      $_delay[_str[46].to_i] = [ _str[46].to_i , _str[2] , -1];
    )
    end
    
    # record receiving time
    if( _str[0] == "r" &&  _str[30] == $_from && _str[32] == $_to) then
    ( 
      $_delay[_str[46].to_i][2] = _str[2] ; 
    )
    end
  )
}

# write each item to file

$_delay.each{ |elem| 
  _id= "%4d" % elem[0].to_i ;
  _d = elem[2].to_f-elem[1].to_f;
  _d = "%.6f" % _d;
  _d = _d.to_s;
  _s = elem[1];
  _s = "%.6f" % _s;
  _r = elem[2];
  _r = "%.6f" % _r;
  _log_msg = "id/s/r/d: " + _id + " " + _s + " " + _r + " " + _d + "\n";
  $_log.puts(_log_msg);
}

$_log.close();

#print("th: "+ $_throughput.to_s+ "   num" + $_num.to_s);

